//
//  GameView.swift
//  StudyDY
//
//  Created by yinchangming on 2018/7/6.
//  Copyright © 2018年 yinchangming. All rights reserved.
//

import UIKit
let kGameCell="kGameCell"
class GameView: UIView {
    
    @IBOutlet weak var gameCollectionView: UICollectionView!
    var isFromFrist=true //是否来自全部页面
    var anchor : [AnchorCatgary]?{
        didSet{
            //来自所有游戏页面就不用删除第一二组
            //因为 这里的数据有可能用首页的分类数据 所以删除 第一 二组（热门 与颜值）
            if(isFromFrist){
                anchor?.removeFirst()
                anchor?.removeFirst()
                //添加 最后一组更多
                let itemLast=AnchorCatgary()
                itemLast.tag_name="更多"
                itemLast.small_icon_url="home_more_btn"
                anchor?.append(itemLast)
            }
            gameCollectionView.reloadData()
        }
    }
    
    override func awakeFromNib() {
        autoresizingMask=[]
        
        gameCollectionView.register(UINib(nibName: "GameCollectionCell", bundle:nil ), forCellWithReuseIdentifier: kGameCell)
        gameCollectionView.contentInset=UIEdgeInsets(top: 0, left: kCollctionMarging, bottom: 0, right: kCollctionMarging)
        gameCollectionView.isPagingEnabled=false
    }
}
//内方法创建对象
extension GameView{
    class func creatView() -> GameView{
        return Bundle.main.loadNibNamed("GameView", owner: nil, options: nil)?.first as! GameView
    }
}
//设置datasoure
extension GameView :UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return anchor?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let item = anchor![indexPath.item]
        let cell=collectionView.dequeueReusableCell(withReuseIdentifier: kGameCell, for: indexPath) as! GameCollectionCell
        cell.catgary=item
        return cell
    }
    
    
}
